//! 多产品类型示例
//! 
//! 展示如何为不同的产品类型和环境创建客户端

use binance_sdk_unified::{
    client::BinanceClientBuilder, ProductType, Environment, Result,
};

#[tokio::main]
async fn main() -> Result<()> {
    // 初始化日志
    tracing_subscriber::fmt::init();

    println!("🚀 Binance多产品类型SDK示例");
    println!("================================");

    // 测试不同的产品类型和环境组合
    let test_cases = vec![
        ("现货-生产网", ProductType::Spot, Environment::Live),
        ("现货-Demo模式", ProductType::Spot, Environment::Demo),
        ("USDT期货-生产网", ProductType::UsdM, Environment::Live),
        ("USDT期货-Demo模式", ProductType::UsdM, Environment::Demo),
        ("币本位期货-生产网", ProductType::CoinM, Environment::Live),
        ("币本位期货-Demo模式", ProductType::CoinM, Environment::Demo),
        ("期权-生产网", ProductType::Options, Environment::Live),
        ("期权-Demo模式", ProductType::Options, Environment::Demo),
    ];

    for (name, product_type, environment) in test_cases {
        println!("\n📋 测试: {}", name);
        println!("   产品类型: {:?}", product_type);
        println!("   环境: {:?}", environment);
        
        // 添加环境日志
        tracing::info!("[MULTI_PRODUCT] 测试: {}", name);
        tracing::info!("[MULTI_PRODUCT] 产品类型: {:?}", product_type);
        tracing::info!("[MULTI_PRODUCT] 环境: {:?}", environment);
        
        // 创建客户端
        let client = BinanceClientBuilder::new(
            "test_api_key".to_string(),
            "test_secret_key".to_string(),
            product_type,
        )
        .environment(environment)
        .timeout(30)
        .retry_count(3)
        .build()?;

        // 测试公共API
        match client.rest().market().get_server_time().await {
            Ok(server_time) => println!("   ✅ 服务器时间: {}", server_time.server_time),
            Err(e) => println!("   ❌ 获取服务器时间失败: {}", e),
        }
    }

    println!("\n🎯 特定产品类型测试");
    println!("==================");

    // 现货测试网详细测试
    println!("\n📊 现货测试网详细测试:");
    let spot_client = BinanceClientBuilder::new(
        "test_api_key".to_string(),
        "test_secret_key".to_string(),
        ProductType::Spot,
    )
    .environment(Environment::Demo)
    .timeout(30)
    .retry_count(3)
    .build()?;

    // 测试各种API
    let apis_to_test = vec![
        ("服务器时间", "get_server_time"),
        ("交易对信息", "get_exchange_info"),
        ("24小时价格", "get_24hr_ticker"),
    ];

    for (api_name, _) in apis_to_test {
        match api_name {
            "服务器时间" => {
                match spot_client.rest().market().get_server_time().await {
                    Ok(server_time) => println!("   ✅ {}: {}", api_name, server_time.server_time),
                    Err(e) => println!("   ❌ {}: {}", api_name, e),
                }
            },
            "交易对信息" => {
                match spot_client.rest().market().get_exchange_info().await {
                    Ok(exchange_info) => println!("   ✅ {}: {} 个交易对", api_name, exchange_info.symbols.len()),
                    Err(e) => println!("   ❌ {}: {}", api_name, e),
                }
            },
            "24小时价格" => {
                match spot_client.rest().market().get_24hr_ticker(Some("BTCUSDT")).await {
                    Ok(ticker) => println!("   ✅ {}: BTCUSDT = {}", api_name, ticker.last_price),
                    Err(e) => println!("   ❌ {}: {}", api_name, e),
                }
            },
            _ => {}
        }
    }

    // USDT期货测试网详细测试
    println!("\n📈 USDT期货测试网详细测试:");
    let futures_client = BinanceClientBuilder::new(
        "test_api_key".to_string(),
        "test_secret_key".to_string(),
        ProductType::UsdM,
    )
    .environment(Environment::Demo)
    .timeout(30)
    .retry_count(3)
    .build()?;

    // 测试期货API
    match futures_client.rest().market().get_server_time().await {
        Ok(server_time) => println!("   ✅ 期货服务器时间: {}", server_time.server_time),
        Err(e) => println!("   ❌ 期货服务器时间: {}", e),
    }

    match futures_client.rest().market().get_24hr_ticker(Some("BTCUSDT")).await {
        Ok(ticker) => println!("   ✅ 期货BTCUSDT价格: {}", ticker.last_price),
        Err(e) => println!("   ❌ 期货BTCUSDT价格: {}", e),
    }

    println!("\n🎉 多产品类型测试完成！");
    println!("================================");

    Ok(())
}
